package cn.com.yocat.flowable.controller;

import cn.com.yocat.common.core.domain.entity.SysUser;
import cn.com.yocat.common.utils.SecurityUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.IdentityService;
import org.flowable.idm.api.Group;
import org.flowable.idm.api.User;
import org.flowable.ui.common.model.GroupRepresentation;
import org.flowable.ui.common.model.ResultListDataRepresentation;
import org.flowable.ui.common.model.UserRepresentation;
import org.flowable.ui.common.security.DefaultPrivileges;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhoushijie
 */
@Slf4j
@RestController
public class FlowableController {

    /**
     * 设计流程图时获取用户组
     */
    @RequestMapping(value = "/rest/editor-groups", method = RequestMethod.GET)
    public ResultListDataRepresentation getGroups(@RequestParam(required = false, value = "filter") String filter) {
        if (StringUtils.isNotBlank(filter)) {
            filter = filter.trim();
            String sql = "select * from ACT_ID_GROUP where NAME_ like #{name} or TYPE_ like #{name}";
            filter = "%" + filter + "%";
            List<Group> groups = this.identityService.createNativeGroupQuery().sql(sql).parameter("name", filter).listPage(0, 10);
            List<GroupRepresentation> result = new ArrayList<>();
            for (Group group : groups) {
                result.add(new GroupRepresentation(group));
            }
            return new ResultListDataRepresentation(result);
        }
        return null;
    }

    /**
     * 设计流程图时获取用户
     */
    @RequestMapping(value = "/rest/editor-users", method = RequestMethod.GET)
    public ResultListDataRepresentation getUsers(@RequestParam(value = "filter", required = false) String filter) {
        if (StringUtils.isNotBlank(filter)) {
            filter = filter.trim();
            String sql = "select * from ACT_ID_USER where ID_ like #{id} or LAST_ like #{name} or FIRST_ like #{name}";
            filter = "%" + filter + "%";
            List<User> matchingUsers = this.identityService.createNativeUserQuery().sql(sql).parameter("id", filter).parameter("name", filter).listPage(0, 10);
            List<UserRepresentation> userRepresentations = new ArrayList<>(matchingUsers.size());
            for (User user : matchingUsers) {
                userRepresentations.add(new UserRepresentation(user));
            }
            return new ResultListDataRepresentation(userRepresentations);
        }
        return null;
    }

    /**
     * 获取当前用户信息
     */
    @RequestMapping(value = "/login/rest/account", method = RequestMethod.GET, produces = "application/json")
    public UserRepresentation getAccount() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        UserRepresentation userRepresentation = new UserRepresentation();
        userRepresentation.setId(user.getUserName());
        userRepresentation.setFirstName(user.getUserName());
        userRepresentation.setLastName(user.getNickName());
        userRepresentation.setEmail(user.getEmail());
        userRepresentation.setFullName(user.getNickName());
        List<String> privileges = new ArrayList<>();
        privileges.add(DefaultPrivileges.ACCESS_MODELER);
        privileges.add(DefaultPrivileges.ACCESS_IDM);
        privileges.add(DefaultPrivileges.ACCESS_ADMIN);
        privileges.add(DefaultPrivileges.ACCESS_TASK);
        privileges.add(DefaultPrivileges.ACCESS_REST_API);
        userRepresentation.setPrivileges(privileges);
        return userRepresentation;
    }

    @Resource
    private ObjectMapper objectMapper;
    @Resource
    protected IdentityService identityService;

}
